package com.syp.hehe.web.controller;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.syp.hehe.domain.vo.CommentVo;
import com.syp.hehe.service.CommentServiceI;
import com.syp.hehe.util.Constants;
import com.syp.hehe.util.ProcessResult;
import com.syp.hehe.util.paging.PageOffSet;

@Controller
@RequestMapping({ "/comment" })
public class CommentController {
	
	private static final Logger logger = LoggerFactory.getLogger(CommentController.class);
	
	@Autowired
	private CommentServiceI commentService;
	
	@RequestMapping(value = "/dcomment")
	@ResponseBody
	public ProcessResult dcomment(@RequestBody CommentVo comment) {
		ProcessResult pr = new ProcessResult();
		try {
			comment.setUuid(UUID.randomUUID().toString());
			this.commentService.insert(comment);
			comment = this.commentService.getByUUID(comment.getUuid());
			pr.setSuccess(true);
			pr.setMessage("评论成功。");
			pr.getRet().put("comment", comment);
		} catch (Exception e) {
			logger.error("评论失败。", e);

			pr.setSuccess(false);
			pr.setMessage("评论失败。");
		}
		return pr;
	}
	
	@RequestMapping("/list/after/{referId}")
	@ResponseBody
	public ProcessResult list(Long articleId, @PathVariable(value = "referId") Long referId) {	
		ProcessResult pr = new ProcessResult();
		try {
			PageOffSet<CommentVo> pageOffSet = this.commentService.getPageOffSetList(articleId, referId, Constants.COMMENT_PIGE_SIZE);

			pr.setSuccess(true);
			pr.setMessage("查询评论列表成功。");
			pr.getRet().put("page", pageOffSet);
		} catch (Exception e) {
			logger.error("查询评论列表失败。", e);

			pr.setSuccess(false);
			pr.setMessage("查询评论列表失败。");
		}
		return pr;
	}
	
	
	
}
